//
//  WJGiftGroupView.m
//  WJAgroManagerDemo
//
//  Created by tqh on 2020/4/7.
//  Copyright © 2020 tqh. All rights reserved.
//

#import "WJGiftGroupView.h"
#import "WJGiftGroupItemView.h"

@interface WJGiftGroupView()

@property (nonatomic,assign) NSInteger index;

@property (nonatomic,strong) NSMutableArray *array;

@end

@implementation WJGiftGroupView

- (void)addGiftWithModel:(WJGiftGroupItemModel *)model {
    __block WJGiftGroupItemView *view = nil;
       [self.array enumerateObjectsUsingBlock:^(WJGiftGroupItemView *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           if ([obj.model.giftName isEqualToString:model.giftName] &&
               obj.model.userId == model.userId) {
               view = obj;
               *stop = YES;
           }
       }];
       
       
       if (!view) {
           WJGiftGroupItemView *view = [[WJGiftGroupItemView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
           view.model = model;
           view.spaceHeight = 5;
           [view setDeleteBlock:^(UIView *view1) {
              [self.array removeObject:view1];
           }];
           [self addSubview:view];
           [self.array addObject:view];
           [view resetLocation];

           [self.array enumerateObjectsUsingBlock:^(WJGiftGroupItemView *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               //用于修复第二层的显示
               if (obj.index == 2 && self.array.count == 2) {
               }else {
                [obj changeLocation];
               }
           }];
           
           
       }else {
           view.model = model;
       }
}


- (void)updateGiftTime {
 
}

#pragma mark - 懒加载

- (NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

@end

